package com.sihan.framework.etm.common.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class FileUtil {

	public static Map<String, String> uploadFile(MultipartFile file, String fileUrl) {
		String fileName = file.getOriginalFilename();
		String savePathAndFileName = fileUrl + File.separator + fileName;
		File uploadFolder = new File(fileUrl);
		if (!uploadFolder.exists() && !uploadFolder.isDirectory()) {
			uploadFolder.mkdir();
		}
		if (!file.isEmpty()) {
			InputStream in = null;
			try (FileOutputStream outputStream = new FileOutputStream(savePathAndFileName)){
				in = file.getInputStream();
				int b = 0;
				while ((b = in.read()) != -1) { // 读取文件
					outputStream.write(b);
				}
			} catch (FileNotFoundException e) {
				log.error("文件处理异常",e);
			} catch (IOException e) {
				log.error("文件处理异常",e);
			}finally{
				if(null != in) {
					try {
						in.close();
					}catch(Exception e) {
						log.error("关闭文件流异常",e);
					}
				}
			}
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("fileUrl", fileUrl);
		map.put("fileName", fileName);
		return map;
	}

	public static void downloadFile(String fileName, String fileUrl, HttpServletResponse response) {
        if (fileName != null) {
            //设置文件路径
            File file = new File(fileUrl+"/"+fileName);
            if (file.exists()) {
                response.setHeader("content-type", "application/octet-stream");
                response.setContentType("application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;filename="+new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                } catch (Exception e) {
                	log.error("文件处理异常",e);
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                        	log.error("文件处理异常",e);
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                        	log.error("文件处理异常",e);
                        }
                    }
                }
            }
        }
	}
}
